package com.zjl.cms.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import com.sun.scenario.effect.Blend.Mode;
import com.zjl.cms.base.model.Const;
import com.zjl.cms.base.model.Paging;
import com.zjl.cms.base.model.Views;
import com.zjl.cms.model.User;
import com.zjl.cms.service.FeedsService;
import com.zjl.cms.service.UserService;

import mblog.shiro.authc.AccountSubject;

@Controller
public class UserController extends BaseController {

	@Resource(name = "userService")
	private UserService userService;
	
	@Resource(name = "feedsService")
	private FeedsService feedsService;
	/**
	 * 跳转登录页
	 * 
	 * @return
	 */
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String index() {
		return getView(Views.LOGIN);
	}

	// public ModelAndView index(){
	// ModelAndView view =new ModelAndView("default/login");
	// view.addObject("name", "lumingfeng");
	// return view;
	// }
	/**
	 * 提交登录
	 * 
	 * @param username
	 * @param password
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(User user, @RequestParam(value = "rememberMe",defaultValue = "0") int rememberMe,ModelMap model) {

		String ret = getView(Views.LOGIN);

		if (StringUtils.isBlank(user.getUsername()) || StringUtils.isBlank(user.getPassword())) {
			return ret;
		}
		
		AuthenticationToken token = createToken(user);
        if (token == null) {
        	model.put("message", "用户名或密码错误");
            return ret;
        }

        if (rememberMe == 1) {
            ((UsernamePasswordToken) token).setRememberMe(true);
        }

        try {
            SecurityUtils.getSubject().login(token);

            ret = Views.REDIRECT_HOME;

            // 更新消息数量
//            pushBadgesCount();
        } catch (AuthenticationException e) {
            if (e instanceof UnknownAccountException) {
            	model.put("message", "用户不存在");
            } else if (e instanceof LockedAccountException) {
            	model.put("message", "用户被禁用");
            } else {
            	e.printStackTrace();
            	model.put("message", "用户认证失败");
            }
        }

        return ret;
        
//		List re = userService.validationUser(user);
//		if (re != null && re.size() == 1) {
//			user = ((User) re.get(0));
//			if (user.getPassword().equals(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()))) {
//				model.put("message", "用户名或密码错误");
//			} else {
//				model.addFlashAttribute("user", user);
//				//System.out.println(user.get);
//				ret = Views.REDIRECT_HOME;
//			}
//		} else if (re == null || re.size() == 0) {
//			model.put("message", "用户信息不存在");
//		} else {
//			model.put("message", "存在一个或多个用户信息，系统暂时无法登陆");
//		}
//
//		return ret;
	}

	/**
	 * 用户主页
	 * 
	 * @param pn
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/home")
	public String home(Integer pn, ModelMap model) {
		Paging paging = wrapPage(pn);
		AccountSubject subject = getSubject();

		feedsService.findUserFeeds(paging, subject.getUser().getId());

		model.put("page", paging);
		initUser(model);
		return getView(Views.HOME_FEEDS);
	}
}
